/**
 * Copyright � 2012 GestureSoft. ALL RIGHTS RESERVED.
 * This file contains proprietary and GestureSoft CONFIDENTIAL Information.
 * Use, disclosure or reproduction is prohibited.
 * 
 * @fileName Companies.as
 * @creationDate Aug 8, 2012
 * @author 
 * @version 1.0
 */
package com.gesturesoft.imultimax.model.utility
{
	
	import com.gesturesoft.imultimax.model.DataBaseManager;
	import com.gesturesoft.imultimax.model.error.DataBaseError;
	import com.gesturesoft.imultimax.model.interfaces.IDatabaseManager;
	
	import mx.collections.ArrayCollection;

	public class Companies
	{
		
		public function Companies()
		{
			
		}
		
		public static function getCompanies():ArrayCollection
		{
			
			var arr:ArrayCollection = new ArrayCollection();
			arr.addItem(getStartUpCompany());
			var dbManager:IDatabaseManager = new DataBaseManager();
			var query:String = "SELECT cmp_company FROM IM_COMPANIES WHERE cmp_startup<>'1'";
			arr.addAll(getCompanyNames(retrieveHandler(query)));
			return arr;
			
		}
		
		public static function getStartUpCompany():String
		{
			
			var dbManager:IDatabaseManager = new DataBaseManager();
			
			var query:String = "SELECT * FROM IM_COMPANIES WHERE cmp_startup='1'";
			var arrCol:ArrayCollection = retrieveHandler(query);
			if(arrCol.length > 0)
				return arrCol[0].cmp_company;
			else
				return null;
			
		}
		
		public static function getStartUpLogo():String
		{
			
			var imgPath:String = null; 
			var startUpCompany:String = getStartUpCompany();
			if( startUpCompany!=null )
			{
				
				var dbManager:IDatabaseManager = new DataBaseManager();
				dbManager.prepareTable("companies");
				var query:String = "SELECT as_value FROM IM_ASSETS WHERE as_company='"+ getStartUpCompany()+"'";
				var arrCol:ArrayCollection = retrieveHandler(query);
				if(arrCol.length > 0)
					imgPath =  getStartUpCompany()+"/"+arrCol[0].as_value;
				else
					imgPath = null;
				
			}
			
			return imgPath;
			
		}
		
		private static function getCompanyNames(value:ArrayCollection):ArrayCollection
		{
			
			var arrCol:ArrayCollection = new ArrayCollection();
			for each(var item:Object in value)
			{
				
				arrCol.addItem(item.cmp_company);
				
			}
			
			return arrCol;
			
		}
		
		private static function retrieveHandler(val:String):ArrayCollection
		{
			
			var result:ArrayCollection;
			
			var _iDBManager:IDatabaseManager = new DataBaseManager();
			try
			{
				
				result = ArrayCollection(_iDBManager.getData(val));
				
			} 
			catch(error:DataBaseError) 
			{
				
				trace("Application-retrieveHandler() : Database Read error");
				throw error;
				
			}
			
			return result;
			
		}
		
	}
	
}